﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

public partial class Admin_Components_Permission_ResetPassword : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DBDataContext db = LINQUtil.GetDBWriteOnly();
            Utils.LoadDataToCombo(cboUserName, "Combo_User", DbType.Int32, 0);
            cboUserName.Items.Insert(0, new RadComboBoxItem("---Chọn---", ""));
            cboUserName.SelectedIndex = 0;
            
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            DBDataContext db = LINQUtil.GetDBWriteOnly();
            string defaultPassword = "123456";
            string PasswordMD5 = Utils.Encrypt(defaultPassword);
            var user = db.Users.FirstOrDefault<User>(u=>u.ID == int.Parse(this.cboUserName.SelectedValue));
            if (user != null)
            {
                user.Password = PasswordMD5;
                user.CreatedBy = Session["UserName"].ToString();
                user.LastUpdatedBy = Session["UserName"].ToString();
                user.CreatedDateTime = DateTime.Now;
                user.LastUpdatedDateTime = DateTime.Now;
                user.Status = 'P';
                db.Update<User>(user);
                db.SubmitChanges();
                this.txtPassword.Text = defaultPassword;
                JavascriptUtil.LoadMessage(UpdatePanel1, true, "Thành công !");
                this.btnSave.Enabled = false;
            }
            else
            {
                JavascriptUtil.LoadMessage(UpdatePanel1, false, "User name này không tồn tại trong hệ thống");
            }
        }
        catch (Exception ex)
        {
            this.btnSave.Enabled = true;
            JavascriptUtil.LoadMessage(UpdatePanel1, false, "Có lỗi xảy ra!");
            ControlUtil.LogError("-----------------------------");
            ControlUtil.LogError("ResetPasswor-btnSave_Click");
            ControlUtil.LogError(ex.ToString());
            ControlUtil.LogError("-----------------------------");
        }
    }
}